﻿Public Class XLLuongNV
    Private xlcc As XLChamCong
    Private xlnv As XLNhanVien
    Private kn As KNLuongNV
    Public Function BUS_AllLuong(ByVal thang As String)
        kn = New KNLuongNV
        Return kn.ALL_DSL(thang)
    End Function
    Public Function BUS_DSLuong() As List(Of DTO_LuongNV.LuongNV)
        Dim ds As New List(Of DTO_LuongNV.LuongNV)
        Dim arrnv As New ArrayList
        arrnv = xlnv.BUS_ArrDSNV()
        For Each nv As DTO_NhanVienCT.NhanVien In arrnv
            Dim l As New DTO_LuongNV.LuongNV
            l.MaNV = nv.MaNV
            l.HoTen = nv.HoTen
            l.CVu = nv.ChucVu
            ds.Add(l)
        Next
        Return ds
    End Function
    Public Function BUS_TaoMoi(ByRef dataS As DataTable, ByVal thang As String, ByRef dsnv As List(Of DTO_NhanVienCT.NhanVien)) As DataTable
        kn = New KNLuongNV
        xlnv = New XLNhanVien
        xlcc = New XLChamCong
        Dim kncc As New KNChamCong
        Dim data As New DataTable
        Dim dataL As New DataTable
        data = kncc.DataCTThang(thang)
        Dim hs As New KNHeSo
        Dim MLuong As String = "ML0"
        Dim arrnv As New ArrayList
        Dim tncn, yt, xh, dktncn As Double
        Dim arrts As New ArrayList
        Dim xlts As New XLThamSo
        arrts = xlts.BUS_DKThamSo()
        For Each n As DTO_ThamSo In arrts
            dktncn = n.DKTCN
            tncn = n.TNCN
            yt = n.BHYT
            xh = n.BHXH
            Exit For
        Next
        If data.Rows.Count <> 0 Then
            dataL = kn.DKDSL(thang)
            arrnv = xlnv.BUS_ArrDSNV()
            For Each nv As DTO_NhanVienCT.NhanVien In arrnv
                Dim ss As Boolean = False
                For Each d As DataRow In data.Rows
                    If d("MaNV") = nv.MaNV Then
                        ss = True
                        Exit For
                    End If
                Next
                If ss = False Then
                    Dim dr As DataRow = data.NewRow
                    dr("MLuong") = MLuong
                    nv.Mluong = MLuong
                    data.Rows.Add(dr)
                    dr = dataL.NewRow
                    Dim nghi As Integer = 0
                    Dim lam As Integer = 0
                    xlcc.BUS_Dem(nv.MaNV, nghi, lam, thang)
                    dr("NgCong") = lam
                    dr("NgNghi") = nghi
                    Dim xl As New XLHopDong
                    Dim lcb As Double = xl.BUS_LCBHD(nv.MaNV)
                    Dim pc As String = ""
                    Dim tien As Double = 0
                    xl.BUS_PCHD(nv.MaNV, pc, tien)
                    dr("TPCap") = pc
                    dr("PCap") = tien
                    dr("LCB") = lcb
                    dr("TTNCN") = tncn
                    dr("BHYT") = yt
                    dr("BHXH") = xh
                    dr("MLuong") = MLuong
                    dr("HSo") = hs.DKDSHS(nv.MaNV)
                    Dim l As Double = lcb / 30
                    Dim t As Double = tncn / 100
                    Dim y As Double = yt / 100
                    Dim x As Double = xh / 100
                    If lam <> 0 Then
                        If dktncn <> lcb Then
                            Dim s As Double = (l * lam) / (t + y + x)
                            If dr("HSo") <> "0" Then dr("TongThuNhap") = tien * Convert.ToDouble(dr("HSo")) Else dr("TongThuNhap") = s + tien
                        Else
                            Dim s As Double = (l * lam) / (y + x)
                            If dr("HSo") <> "0" Then dr("TongThuNhap") = tien * Convert.ToDouble(dr("HSo")) Else dr("TongThuNhap") = s + tien
                        End If
                    Else
                        dr("TongThuNhap") = 0
                    End If
                    dataL.Rows.Add(dr)
                    dsnv.Add(nv)
                    xlcc.BUS_MMT(MLuong)
                End If
            Next
        Else
            arrnv = xlnv.BUS_ArrDSNV()
            dataL = kn.DSL()
            For Each nv As DTO_NhanVienCT.NhanVien In arrnv
                Dim dr As DataRow = data.NewRow
                dr("Thang") = thang
                dr("MaNV") = nv.MaNV
                dr("MLuong") = MLuong
                data.Rows.Add(dr)
                dr = dataL.NewRow
                Dim nghi As Integer = 0
                Dim lam As Integer = 0
                xlcc.BUS_Dem(nv.MaNV, nghi, lam, thang)
                nv.Mluong = MLuong
                dr("NgCong") = lam
                dr("NgNghi") = nghi
                Dim xl As New XLHopDong
                Dim lcb As Double = xl.BUS_LCBHD(nv.MaNV)
                Dim pc As String = ""
                Dim tien As Double = 0
                xl.BUS_PCHD(nv.MaNV, pc, tien)
                dr("TPCap") = pc
                dr("PCap") = tien
                dr("LCB") = lcb
                dr("TTNCN") = tncn
                dr("BHYT") = yt
                dr("BHXH") = xh
                dr("MLuong") = MLuong
                dr("HSo") = hs.DKDSHS(nv.MaNV)
                Dim l As Double = lcb / 30
                Dim t As Double = tncn / 100
                Dim y As Double = yt / 100
                Dim x As Double = xh / 100
                If lam <> 0 Then
                    If dktncn <> lcb Then
                        Dim s As Double = (l * lam) / (t + y + x)
                        If dr("HSo") <> "0" Then dr("TongThuNhap") = tien * Convert.ToDouble(dr("HSo")) Else dr("TongThuNhap") = s + tien
                    Else
                        Dim s As Double = (l * lam) / (y + x)
                        If dr("HSo") <> "0" Then dr("TongThuNhap") = tien * Convert.ToDouble(dr("HSo")) Else dr("TongThuNhap") = s + tien
                    End If
                Else
                    dr("TongThuNhap") = 0
                End If
                dataL.Rows.Add(dr)
                dsnv.Add(nv)
                xlcc.BUS_MMT(MLuong)
            Next
        End If
        kn.SaveLuong(dataL)
        kn.SaveCTiet(data)
        Return dataL
    End Function
   
End Class
